﻿using System;
using System.Collections.Generic;
using System.Security.Claims;
using System.Text;
using Castle.DynamicProxy;
using Common.Attributes;
using Common.Mongodb;
using Common.RabbitMQ;
using Domian;
using Host.Configuration;
using Host.JWT;
using IDBContext;
using IServices;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.IdentityModel.Tokens;
using Microsoft.Net.Http.Headers;

namespace Host
{
    /// <summary>
    /// 测试控制器
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        private JwtToken _token;
        private IBaseService<Test> _service;
        private readonly RabbitMQHelp _rabbitMQHelp;
        private readonly IConfiguration _configuration;

        public ValuesController(JwtToken token, IBaseService<Test> service, RabbitMQHelp rabbitMQHelp, IConfiguration configuration)
        {
            _token = token;
            _service = service;
            _rabbitMQHelp = rabbitMQHelp;
            _configuration = configuration;
        }

        [ApiExplorerSettings(IgnoreApi = true)]
        public TokenConfigurations GetConfigurations()
        {
            var key = new SymmetricSecurityKey(Encoding.ASCII.GetBytes("ROURS_RBKAXB10086110.ROURSSAXB"));
            return new TokenConfigurations
            {
                SecurityKey = key,
                Issuer = "ASP.NetCore",
                Audience = "ASP.NetCore",
                SigningCredentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256),
                Expiration = TimeSpan.FromHours(1)
            };
        }

        /// <summary>
        /// 测试接口
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Post() => Ok(_service.Test());

        /// <summary>
        /// 获取JWT的Token
        /// </summary>
        /// <returns></returns>
        [HttpGet("/GetToken")]
        public IActionResult GetToken()
        {
            //添加一个Claim列表
            var claims = new List<Claim>();
            claims.Add(new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier", "axb"));
            claims.Add(new Claim(ClaimTypes.Name, "axb"));
            //claims.Add(new Claim(ClaimTypes.Role, "Admin"));

            return Ok(_token.GetToken(new ClaimsIdentity(claims)));
        }

        /// <summary>
        /// 获取加密的JWT的Token
        /// </summary>
        /// <returns></returns>
        [HttpGet("/GetEncryptToken")]
        public IActionResult GetEncryptToken()
        {
            //添加一个Claim列表
            var claims = new List<Claim>();
            claims.Add(new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier", "axb"));

            return Ok(_token.GetEncryptToken(new ClaimsIdentity(claims)));
        }

        /// <summary>
        /// 解析Token，获取信息
        /// </summary>
        /// <returns></returns>
        [HttpGet("/SerializeToken")]
        //[Authorize("Admin")]
        [Authorize]
        public IActionResult SerializeToken()
        {
            if (HttpContext.Request.Headers.ContainsKey(HeaderNames.Authorization))
            {
                return Ok(_token.SerializeToken(HttpContext.Request.Headers[HeaderNames.Authorization].ToString().Replace("Bearer ", "")));
            }
            return Ok(false);
        }

        /// <summary>
        /// 生产RabbitMQ消息
        /// </summary>
        /// <param name="queue"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        [HttpPost("/Send")]
        public IActionResult SendMessage(string queue,string msg)
        {
            _rabbitMQHelp.SendMessage(queue, msg);
            return Ok();
        }

        /// <summary>
        /// 消费RabbitMQ消息
        /// </summary>
        /// <param name="queue"></param>
        /// <returns></returns>
        [HttpGet("/Receive")]
        public IActionResult ReceiveMessage(string queue)
        {
            var result = _rabbitMQHelp.ReceiveMessage(queue);
            return Ok(result);
        }

        /// <summary>
        /// 添加Mongodb数据
        /// </summary>
        /// <param name="name"></param>
        /// <param name="age"></param>
        /// <returns></returns>
        [HttpGet("/AddData")]
        public IActionResult AddMongodbData(string name,int age)
        {
            var mongoHelp = new MongodbHelp<User>(_configuration);
            var entity = new User
            {
                Id = Guid.NewGuid(),
                Name = name,
                Age = age
            };
            mongoHelp.InsertAsync(entity);
            return Ok();
        }
    }
}
